package com.jiujia.game.sdk.login.ui.dialog;

import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.Display;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.jiujia.game.sdk.base.JGameActivityManager;
import com.jiujia.game.sdk.base.util.JGameLog;
import com.jiujia.game.sdk.login.R;

/**
 * *************************************************
 * Class: TermsAndConditionsDialog
 * Author: Jiujia
 * Time: 2025/5/5 11:16
 * Description: 用户和隐私协议
 * *************************************************
 */
public class TermsDialog {

    private Dialog dialog;
    private LinearLayout main_terms_conditions;
    private TextView tv_title_content;
    private TextView tv_terms_content;
    private Button btn_terms_refuse, btn_terms_agree;

    private Display display;

    public TermsDialog() {

        WindowManager windowManager = (WindowManager) JGameActivityManager.getActivity()
                .getSystemService(JGameActivityManager.getActivity().WINDOW_SERVICE);
        display = windowManager.getDefaultDisplay();
    }

    public TermsDialog builder() {
        View view = LayoutInflater.from(JGameActivityManager.getActivity()).inflate(
                R.layout.j_game_dialog_terms_conditions, null);
        main_terms_conditions = view.findViewById(R.id.main_terms_conditions);
        tv_title_content = view.findViewById(R.id.tv_title_content);
        tv_terms_content = view.findViewById(R.id.tv_terms_content);
        btn_terms_refuse = view.findViewById(R.id.btn_terms_refuse);
        btn_terms_agree = view.findViewById(R.id.btn_terms_agree);
        tv_title_content.setText(R.string.j_game_terms);
        setupClickableTerms(tv_terms_content);

        btn_terms_refuse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
                JGameActivityManager.finish();
            }
        });


        dialog = new Dialog(JGameActivityManager.getActivity(), R.style.GameDialogStyle);
        dialog.setContentView(view);
        dialog.setCanceledOnTouchOutside(false);

        main_terms_conditions.setLayoutParams(new FrameLayout.LayoutParams((int) (display.getWidth() * 0.9), (int) (display.getHeight() * 0.5)));

        dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {

            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                // TODO Auto-generated method stub
                if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
                    //do something...
                    return true;
                } else {
                    return false;
                }
            }
        });
        return this;
    }

    public void show() {

        dialog.show();
    }

    public TermsDialog setAgreeOnClickListener(View.OnClickListener mOnClickListener) {
        btn_terms_agree.setOnClickListener((view -> {
            mOnClickListener.onClick(view);
            dialog.dismiss();
        }));
        return this;
    }


    private void setupClickableTerms(TextView textView) {
        String part1 = JGameActivityManager.getActivity().getString(R.string.j_game_terms_1);
        String userAgreement = JGameActivityManager.getActivity().getString(R.string.j_game_terms_user);
        String part2 = JGameActivityManager.getActivity().getString(R.string.j_game_terms_2);
        String privacyPolicy = JGameActivityManager.getActivity().getString(R.string.j_game_terms_privacy);
        String part3 = JGameActivityManager.getActivity().getString(R.string.j_game_terms_3);
        String part4 = "\n" + JGameActivityManager.getActivity().getString(R.string.j_game_terms_4);
        String part5 = "\n" + JGameActivityManager.getActivity().getString(R.string.j_game_terms_5);

        String fullText = part1 + userAgreement + part2 + privacyPolicy + part3 + part4 + part5;
        SpannableString spannable = new SpannableString(fullText);

        int userStart = part1.length();
        int userEnd = userStart + userAgreement.length();

        int privacyStart = userEnd + part2.length();
        int privacyEnd = privacyStart + privacyPolicy.length();


        // 设置用户协议点击
        ClickableSpan userAgreementSpan = new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                // 打开用户协议
                JGameLog.d("点击了用户协议");
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                ds.setColor(Color.BLUE); // 设置链接颜色
                ds.setUnderlineText(false); // 去掉下划线
            }
        };

        // 设置隐私政策点击
        ClickableSpan privacyPolicySpan = new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                // 打开隐私政策
                JGameLog.d("点击了隐私政策");
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                ds.setColor(Color.BLUE);
                ds.setUnderlineText(false);
            }
        };

        // 应用Span
        spannable.setSpan(userAgreementSpan, userStart, userEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannable.setSpan(privacyPolicySpan, privacyStart, privacyEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        // 设置文本和点击效果
        textView.setText(spannable);
        textView.setMovementMethod(LinkMovementMethod.getInstance());
        textView.setHighlightColor(Color.TRANSPARENT); // 去掉点击后的背景色

    }
}